spitoi2c 2.2.0
Loading...
Searching...
No Matches
spitoi2c.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2020 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
27
28#ifndef SPITOI2C_H
29#define SPITOI2C_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51
57
62
67
72#define SPITOI2C_CMD_WRITE 0x00
73#define SPITOI2C_CMD_READ 0x01
74#define SPITOI2C_CMD_READ_AFTER_WRITE 0x02
75#define SPITOI2C_CMD_WRITE_AFTER_WRITE 0x03
76#define SPITOI2C_CMD_READ_BUFFER 0x06
77#define SPITOI2C_CMD_SPI_CONFIG 0x18
78#define SPITOI2C_CMD_WRITE_REG 0x20
79#define SPITOI2C_CMD_READ_REG 0x21
80#define SPITOI2C_CMD_DEEP_POWER_DOWN 0x30
81#define SPITOI2C_CMD_READ_VERSION 0xFE
82
87#define SPITOI2C_REG_IO_CONFIG 0x00
88#define SPITOI2C_REG_IO_STATE 0x01
89#define SPITOI2C_REG_I2C_CLOCK 0x02
90#define SPITOI2C_REG_I2C_TO 0x03
91#define SPITOI2C_REG_I2C_STAT 0x04
92#define SPITOI2C_REG_I2C_ADR 0x05
93 // spitoi2c_reg
95
100
105
110#define SPITOI2C_I2C_CLOCK_375_KHZ 5
111#define SPITOI2C_I2C_CLOCK_99_KHZ 19
112#define SPITOI2C_I2C_CLOCK_7p4_KHZ 255
113#define SPITOI2C_I2C_ADDR_127 0xFE
114#define SPITOI2C_I2C_TO_DISABLE 0x00
115
120#define SPITOI2C_I2C_STATUS_SUCCESS 0xF0
121#define SPITOI2C_I2C_STATUS_TARGET_NACK 0xF1
122#define SPITOI2C_I2C_STATUS_WRITE_NACK 0xF2
123#define SPITOI2C_I2C_STATUS_BUSY 0xF3
124#define SPITOI2C_I2C_STATUS_TIMEOUT 0xF8
125#define SPITOI2C_I2C_STATUS_DATA_COUNT 0xF9
126
131#define SPITOI2C_TIMEOUT_MS 3000
132
137#define SPITOI2C_NO_PIN_MASK 0x00
138#define SPITOI2C_PIN_0_MASK 0x01
139#define SPITOI2C_PIN_1_MASK 0x02
140#define SPITOI2C_PIN_2_MASK 0x04
141#define SPITOI2C_PIN_3_MASK 0x08
142#define SPITOI2C_PIN_4_MASK 0x10
143#define SPITOI2C_ALL_PINS_MASK 0x1F
144#define SPITOI2C_ALL_OUT_PINS_MASK 0x0F
145
150#define SPITOI2C_PIN_DIR_INPUT 0x00
151#define SPITOI2C_PIN_DIR_OUTPUT_PP 0x02
152#define SPITOI2C_PIN_DIR_OUTPUT_OD 0x03
153#define SPITOI2C_PIN_DIR_MASK 0x03
154
159#define SPITOI2C_I2C_READ_BIT 0x01
160
165#define SPITOI2C_SPI_CONFIG_LSB_FIRST 0x81
166#define SPITOI2C_SPI_CONFIG_MSB_FIRST 0x42
167
172#define SPITOI2C_DEEP_PDOWN_BYTE_0 0x5A
173#define SPITOI2C_DEEP_PDOWN_BYTE_1 0xA5
174 // spitoi2c_set
176
181
186
191#define SPITOI2C_MAP_MIKROBUS( cfg, mikrobus ) \
192 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
193 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
194 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
195 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
196 cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
197 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
198 // spitoi2c_map // spitoi2c
201
206typedef struct
207{
208 // Output pins
209 digital_out_t mosi;
210 digital_out_t sck;
211 digital_out_t cs;
212 digital_out_t rst;
213
214 // Input pins
215 digital_in_t miso;
216 digital_in_t int_pin;
217
218} spitoi2c_t;
219
224typedef struct
225{
226 // Communication gpio pins
227 pin_name_t miso;
228 pin_name_t mosi;
229 pin_name_t sck;
230 pin_name_t cs;
231
232 // Additional gpio pins
233 pin_name_t rst;
234 pin_name_t int_pin;
235
237
242typedef enum
243{
246
248
254
265
280
294
306err_t spitoi2c_gpio_write ( spitoi2c_t *ctx, uint8_t gpio_data );
307
319err_t spitoi2c_gpio_read ( spitoi2c_t *ctx, uint8_t *gpio_data );
320
335err_t spitoi2c_gpio_config ( spitoi2c_t *ctx, uint8_t direction, uint8_t pin_mask );
336
348err_t spitoi2c_read_i2c_status ( spitoi2c_t *ctx, uint8_t *i2c_stat );
349
364err_t spitoi2c_i2c_write ( spitoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_in, uint8_t len );
365
380err_t spitoi2c_i2c_read ( spitoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_out, uint8_t len );
381
398err_t spitoi2c_i2c_read_after_write ( spitoi2c_t *ctx, uint8_t slave_addr,
399 uint8_t *data_in, uint8_t in_len, uint8_t *data_out, uint8_t out_len );
400
417err_t spitoi2c_i2c_write_after_write ( spitoi2c_t *ctx, uint8_t slave_addr,
418 uint8_t *data_in1, uint8_t in1_len, uint8_t *data_in2, uint8_t in2_len );
419
430void spitoi2c_read_buffer ( spitoi2c_t *ctx, uint8_t *data_out, uint8_t len );
431
443err_t spitoi2c_set_spi_config ( spitoi2c_t *ctx, uint8_t spi_cfg );
444
458err_t spitoi2c_write_register ( spitoi2c_t *ctx, uint8_t reg, uint8_t data_in );
459
472err_t spitoi2c_read_register ( spitoi2c_t *ctx, uint8_t reg, uint8_t *data_out );
473
483
493
505err_t spitoi2c_read_version ( spitoi2c_t *ctx, uint8_t *version );
506
516
526
536
537#ifdef __cplusplus
538}
539#endif
540#endif // SPITOI2C_H
541 // spitoi2c
543
544// ------------------------------------------------------------------------ END
err_t spitoi2c_set_spi_config(spitoi2c_t *ctx, uint8_t spi_cfg)
SPI to I2C set spi config function.
uint8_t spitoi2c_get_int_pin(spitoi2c_t *ctx)
SPI to I2C get int pin function.
err_t spitoi2c_write_register(spitoi2c_t *ctx, uint8_t reg, uint8_t data_in)
SPI to I2C write register function.
void spitoi2c_deep_power_down(spitoi2c_t *ctx)
SPI to I2C deep power down function.
err_t spitoi2c_i2c_write(spitoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_in, uint8_t len)
SPI to I2C i2c write function.
void spitoi2c_wake_up(spitoi2c_t *ctx)
SPI to I2C wake up function.
err_t spitoi2c_i2c_write_after_write(spitoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_in1, uint8_t in1_len, uint8_t *data_in2, uint8_t in2_len)
SPI to I2C i2c write after write function.
err_t spitoi2c_init(spitoi2c_t *ctx, spitoi2c_cfg_t *cfg)
SPI to I2C initialization function.
void spitoi2c_read_buffer(spitoi2c_t *ctx, uint8_t *data_out, uint8_t len)
SPI to I2C read buffer function.
void spitoi2c_enable_device(spitoi2c_t *ctx)
SPI to I2C enable device function.
err_t spitoi2c_default_cfg(spitoi2c_t *ctx)
SPI to I2C default configuration function.
err_t spitoi2c_gpio_config(spitoi2c_t *ctx, uint8_t direction, uint8_t pin_mask)
SPI to I2C gpio gpio config function.
err_t spitoi2c_gpio_read(spitoi2c_t *ctx, uint8_t *gpio_data)
SPI to I2C gpio read function.
err_t spitoi2c_read_i2c_status(spitoi2c_t *ctx, uint8_t *i2c_stat)
SPI to I2C read i2c status function.
void spitoi2c_cfg_setup(spitoi2c_cfg_t *cfg)
SPI to I2C configuration object setup function.
err_t spitoi2c_i2c_read(spitoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_out, uint8_t len)
SPI to I2C i2c read function.
err_t spitoi2c_gpio_write(spitoi2c_t *ctx, uint8_t gpio_data)
SPI to I2C gpio write function.
err_t spitoi2c_i2c_read_after_write(spitoi2c_t *ctx, uint8_t slave_addr, uint8_t *data_in, uint8_t in_len, uint8_t *data_out, uint8_t out_len)
SPI to I2C i2c read after write function.
void spitoi2c_disable_device(spitoi2c_t *ctx)
SPI to I2C disable device function.
err_t spitoi2c_read_version(spitoi2c_t *ctx, uint8_t *version)
SPI to I2C read version function.
err_t spitoi2c_read_register(spitoi2c_t *ctx, uint8_t reg, uint8_t *data_out)
SPI to I2C read register function.
spitoi2c_return_value_t
SPI to I2C Click return value data.
Definition spitoi2c.h:243
@ SPITOI2C_OK
Definition spitoi2c.h:244
@ SPITOI2C_ERROR
Definition spitoi2c.h:245
SPI to I2C Click configuration object.
Definition spitoi2c.h:225
pin_name_t sck
Definition spitoi2c.h:229
pin_name_t mosi
Definition spitoi2c.h:228
pin_name_t int_pin
Definition spitoi2c.h:234
pin_name_t miso
Definition spitoi2c.h:227
pin_name_t rst
Definition spitoi2c.h:233
pin_name_t cs
Definition spitoi2c.h:230
SPI to I2C Click context object.
Definition spitoi2c.h:207
digital_out_t cs
Definition spitoi2c.h:211
digital_out_t sck
Definition spitoi2c.h:210
digital_out_t mosi
Definition spitoi2c.h:209
digital_in_t miso
Definition spitoi2c.h:215
digital_in_t int_pin
Definition spitoi2c.h:216
digital_out_t rst
Definition spitoi2c.h:212